<!DOCTYPE html>
<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link href="style.css" rel="stylesheet" type="text/css">
  </head>
  <body>
      <script type="text/javascript" src="jquery-1.5.2.js"></script>
      <script type="text/javascript" src="jquery.form.js"></script>
      <script type="text/javascript" src="crud.js"></script>
      <script type="text/javascript" src="admin.js"></script>
      <script type="text/javascript">
          function reload() {
              $.getJSON("controllers/login.php",function(data) {
                  for (var i in allCruds) {
                      allCruds[i].container.hide();
                  }
                  if (data['loggedInAs'] == null) {
                      $("#loggedInAs").text("You are not logged in");
                      $('#loginFormDiv').fadeIn(1000);
                  } else {
                      $("#loggedInAs").text("You are logged in as "+data['loggedInAs'].name+" ");
                      $("#loggedInAs").append( $('<a href="">relogin</a>').click(function() { $('#loginFormDiv').fadeIn(1000); return false; }) );
                      $("#loggedInAs").append(" ");
                      $("#loggedInAs").append( $('<a href="">logout</a>').click(function() { 
                          $.post('controllers/login.php',{logout:1},
                            function () { reload(); }
                          );
                          return false;
                      }));
                      if (data['loggedInAs'].admin == 1) {
                          for (var i in adminCruds) {
                              adminCruds[i].crud.refreshRead();
                              adminCruds[i].container.show();
                          }
                      } else if (data['loggedInAs'].employee == 1) {
                          for (var i in employeeCruds) {
                              employeeCruds[i].crud.refreshRead();
                              employeeCruds[i].container.show();
                          }
                      }
                  }
          });
          }

          $(document).ready(function() {
            categories = {
                'container':$('#catsDiv'),
                'crud': defaultCRUD("#crudCats",
			"controllers/book_categories.php",{noinit:true})
            };
            books = {
                'container':$('#booksDiv'),
                'crud': getCrudBooks("#crudBooks","controllers/books.php")
            };
            users = {
                'container': $('#usersDiv'),
                'crud' : getCrudUsers("#crudUsers","controllers/users.php")
            };

            reviews = {
                'container': $('#reviewsDiv'),
                'crud': getCrudReviews("#crudReviews","controllers/reviews.php")
            };

            orders = {
                'container': $('#ordersDiv'),
                'crud': getCrudOrders('#crudOrders')
            };
            
            categories.crud.onRefresh = function() {
		        books.crud.setCategories(this.collection);
	        }

            allCruds = [ reviews, categories, books, users,orders ];
            adminCruds = allCruds;
            employeeCruds = [ reviews, categories, books,orders ];
    
            for (var i in allCruds) {
                allCruds[i].crud.renderHeader();
            }

            $('#loginForm').ajaxForm(function() {
                $('#loginFormDiv').fadeOut(1000);
                reload();
            });
            reload();
          });
    </script>
      
      <div id="loggedIn">
      <p id="loggedInAs"></p>
      <div id="loginFormDiv" style="display: none">
        <form id="loginForm" action="controllers/login.php" method="POST">
      	<table>
              <tr> <td>login: </td> <td><input type="text" name="login"/></td> </tr>
              <tr> <td>password: </td> <td><input type="password" name="pwd"/></td> </tr>
              <tr> <td colspan="2"><input type="submit" value="login"/></td></tr>
        </table>
        </form>
      </div>
      </div>

      <div id="ordersDiv" class="admin" style="display:none">
      <h1>orders</h1>
      <div id="crudOrders">
      </div>
      </div>
      
      <div id="booksDiv" class="admin" style="display:none">
      <h1>books</h1>
      <div id="crudBooks">
      </div>
      </div>

      <div id="catsDiv" class="admin" style="display:none">
      <h1>book categories</h1>
      <div id="crudCats">
      </div>
      </div>


      <div id="usersDiv" class="admin" style="display:none">
      <h1>users</h1>
      <div id="crudUsers">
      </div>
      </div>
    
      <div id="reviewsDiv" class="admin" style="display:none">
      <h1>reviews</h1>
      <div id="crudReviews">
      </div>
      </div>

  </body>
</html>

